import * as excel from '@/vendor/Export2Excel'
import xlsx from 'xlsx'
export function exportUsers (userList) {
  let header = ['序号', '部门', '姓名', '性别', '身份证号', '出生日期', '政治面貌', '籍贯', '职务', '工种', '联系方式', '最高学历', '员工编号']
  let keys = ['index', 'deptName', 'realname', 'genderStr', 'identityCard', 'birthday', 'politicCountenance', 'nativePlace',
    'duty', 'craftName', 'mobile', 'education', 'jobNumber']
  let data = userList.map(x => keys.map(k => x[k]))
  data.forEach((x, i) => (x[0] = i + 1))

  excel.export_json_to_excel({
    title: '人员列表',
    header: header,
    data: data,
    filename: '人员列表'
  })
}

export function exportUserAndCert (dataList) {
  let certCountList = dataList.map(x => x.certUserCertList).sort((a, b) => b.length - a.length)
  let certCount = certCountList.length > 0 ? certCountList[0].length : 0
  let title = ['基本信息', null, null, null, null, null, null, null, null, null, null, null, null]
  let header = ['序号', '部门', '姓名', '性别', '身份证号', '出生日期', '政治面貌', '籍贯', '职务', '工种', '联系方式', '最高学历', '员工编号']
  let merges = [{
    s: {r: 0, c: 0},
    e: {r: 0, c: 11}
  }]

  for (let i = 0; i < certCount; i++) {
    title.push(`证件管理${i + 1}`, null, null, null, null, null, null, null, null, null, null, null, null)

    merges.push({
      s: {r: 0, c: 12 + i * 13 + 1},
      e: {r: 0, c: 12 + (i + 1) * 13}
    })

    header.push(...['证件类型', '证件名称', '专业', '级别',
      '技术等级', '工种', '证件编号', '发证日期', '开始日期', '截止日期',
      '复审日期', '发证机构', '操作项目'])
  }
  let entityList = []
  entityList.push(title)
  entityList.push(header)
  dataList.forEach((item, index) => {
    let entity = [
      index + 1,
      item.userBaseInfo['deptName'],
      item.userBaseInfo['realname'],
      item.userBaseInfo['genderStr'],
      item.userBaseInfo['identityCard'],
      item.userBaseInfo['birthday'],
      item.userBaseInfo['politicCountenance'],
      item.userBaseInfo['nativePlace'],
      item.userBaseInfo['duty'],
      item.userBaseInfo['craftName'],
      item.userBaseInfo['mobile'],
      item.userBaseInfo['education'],
      item.userBaseInfo['jobNumber']
    ]
    let certList = item.certUserCertList
    for (let i = 0; i < certList.length; i++) {
      entity.push(certList[i]['certType'])
      entity.push(certList[i]['certName'])
      entity.push(certList[i]['specialty'])
      entity.push(certList[i]['level'])
      entity.push(certList[i]['skillLevel'])
      entity.push(certList[i]['craft'])
      entity.push(certList[i]['certNumber'])
      entity.push(certList[i]['certOpenDate'])
      entity.push(certList[i]['certStartTime'])
      entity.push(certList[i]['certEndTime'])
      entity.push(certList[i]['certSecondTime'])
      entity.push(certList[i]['certAuthority'])
      entity.push(certList[i]['remark'])
    }
    entityList.push(entity)
  })
  let sheet = xlsx.utils.aoa_to_sheet(entityList)
  sheet['!merges'] = merges
  openDownloadDialog(sheet2blob(sheet), '人员及证件列表.xlsx')
}

export function exportCert (certList) {
  let header = ['姓名', '部门', '身份证号', '证件类型', '证件名称', '专业', '级别',
    '技术等级', '工种', '证件编号', '发证日期', '开始日期', '截止日期',
    '复审日期', '发证机构', '操作项目']

  let keys = ['realname', 'deptName', 'identityCard', 'certType', 'certName', 'specialty', 'level',
    'skillLevel', 'craft', 'certNumber', 'certOpenDate', 'certStartTime', 'certEndTime',
    'certSecondTime', 'certAuthority', 'remark']

  let data = certList.map(x => keys.map(k => x[k]))
  excel.export_json_to_excel({
    title: '证件列表',
    header: header,
    data: data,
    filename: '证件列表'
  })
}

function openDownloadDialog (url, saveName) {
  if (typeof url === 'object' && url instanceof Blob) {
    url = URL.createObjectURL(url) // 创建blob地址
  }
  var aLink = document.createElement('a')
  aLink.href = url
  aLink.download = saveName || '' // HTML5新增的属性，指定保存文件名，可以不要后缀，注意，file:///模式下不会生效
  var event
  if (window.MouseEvent) {
    event = new MouseEvent('click')
  } else {
    event = document.createEvent('MouseEvents')
    event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
  }
  aLink.dispatchEvent(event)
}

function sheet2blob (sheet, sheetName) {
  sheetName = sheetName || 'sheet1'
  var workbook = {
    SheetNames: [sheetName],
    Sheets: {}
  }
  workbook.Sheets[sheetName] = sheet
  // 生成excel的配置项
  var wopts = {
    bookType: 'xlsx', // 要生成的文件类型
    bookSST: false, // 是否生成Shared String Table，官方解释是，如果开启生成速度会下降，但在低版本IOS设备上有更好的兼容性
    type: 'binary'
  }
  var wbout = xlsx.write(workbook, wopts)
  var blob = new Blob([s2ab(wbout)], {type: 'application/octet-stream'})
  // 字符串转ArrayBuffer
  function s2ab (s) {
    var buf = new ArrayBuffer(s.length)
    var view = new Uint8Array(buf)
    for (var i = 0; i !== s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF
    return buf
  }
  return blob
}
